from django.contrib import admin
from blog.models import Article, Column
from django.utils import timezone
admin.site.disable_action('delete_selected')


# Register your models here.
class ArticleAdmin(admin.ModelAdmin):
    def delete_articles(self, request, queryset):
        for obj in queryset:
            print(obj)
            print("delete_articles called")
            c = obj.column
            c.article_num -= 1
            c.update_date = timezone.now()
            c.update_content = obj.title + u" 被删除。"
            c.save()
            obj.delete()

    def delete_model(self, request, obj):
        print("Delete function called")
        c = obj.column
        c.article_num -= 1
        c.update_date = timezone.now()
        c.update_content = obj.title + u" 被删除。"
        c.save()
        super(ArticleAdmin, self).delete_model(request, obj)

    delete_articles.short_description = u"删除所选文章"
    actions = [delete_articles]
    list_display = ['title', 'column', 'pub_date', 'update_time', 'author']


class ColumnAdmin(admin.ModelAdmin):
    list_display = ['column_text', 'create_date', 'update_date', 'article_num', 'update_content']


admin.site.register(Article, ArticleAdmin)
admin.site.register(Column, ColumnAdmin)
